package myDemo.bobo.demo;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
import java.util.Date;
import java.text.*;

public class CourseTable extends JFrame implements ActionListener
{

JLabel label1[]=new JLabel[8];
JPanel panel1=new JPanel();
JPanel panel2=new JPanel();
JPanel panel3=new JPanel();
JPanel panel4=new JPanel();
JPanel titlepanel=new JPanel();
JPanel weekpanel=new JPanel();
JButton initbutton = new JButton("新建");
JButton alterbut=new JButton("修改");
JButton okbut=new JButton("显示");
JButton restbut=new JButton("重置");
JLabel titlabel=new JLabel("计科课程表");
JMenuBar menubar=new JMenuBar();
JMenu filemenu=new JMenu("File");
JMenu editmenu=new JMenu("Edit");
JMenu toolmenu=new JMenu("Tool");
JMenu helpmenu=new JMenu("Help");
JMenuItem useitem=new JMenuItem("Instruction",'I');
JMenuItem aboutitem=new JMenuItem("About 课程表",'A');
JMenuItem inititem = new JMenuItem("Initialize",'I');
JMenuItem displayitem=new JMenuItem("Display",'D');
JMenuItem alteritem=new JMenuItem("Alter",'A');
JMenuItem resetitem=new JMenuItem("Reset",'R');

JLabel timelabel=new JLabel();
Date now=new Date();
DateFormat d=DateFormat.getDateTimeInstance();
String time=d.format(now); 
JTextArea text1[][] =new JTextArea[12][8];
JLabel label2[] = new JLabel[11];
String course[][]=new String[12][8];
JScrollPane spane;
Font font;
public CourseTable()
{
   super("课程表");
  
   String[] fontNames = GraphicsEnvironment
        .getLocalGraphicsEnvironment()
        .getAvailableFontFamilyNames();
   //for (String fontName : fontNames)
       // System.out.println(fontName);

  
   this.setBounds(200,100,800,660);
   Container con=getContentPane();
   this.setJMenuBar(menubar);
  
   font = new Font("华文彩云",Font.BOLD,20);
   titlabel.setFont(font);
   titlabel.setForeground(Color.red);
   spane= new JScrollPane();
  
   con.add(spane);
   spane.setVisible(true);
  
   menubar.add(filemenu);
   menubar.add(editmenu);
   menubar.add(toolmenu);
   menubar.add(helpmenu);
   helpmenu.add(useitem);
   helpmenu.addSeparator();
   helpmenu.add(aboutitem);
   filemenu.add(inititem);
   filemenu.add(displayitem);
   filemenu.addSeparator();
   filemenu.add(alteritem);
   filemenu.add(resetitem);
   filemenu.setMnemonic('F');
   editmenu.setMnemonic('E');
   toolmenu.setMnemonic('T');
   helpmenu.setMnemonic('H');
   aboutitem.addActionListener(
     new ActionListener()
     {
      public void actionPerformed(ActionEvent e)
      {
       JOptionPane.showMessageDialog(null,"课程表   版本1.0\n" +
         "作者：黄炼军\n"+ 
         "时间：2009-7-25\n"+
         "Coldboy 工作室\n"+ 
         "All rights reserved.",
         "About 课程表",1);
      }
     });


   con.setBackground(Color.yellow);
   con.setLayout(new BorderLayout());
   panel1.setLayout(new GridLayout(2,1));
   panel2.setLayout(new GridLayout(11,7,5,5));
   panel3.setLayout(new GridLayout(11,1));
   panel4.setLayout(new FlowLayout());
   weekpanel.setLayout(new GridLayout(1,8));
   con.add(panel1,BorderLayout.NORTH);
   con.add(panel2,BorderLayout.CENTER);
   con.add(panel3,BorderLayout.WEST);
   con.add(panel4,BorderLayout.SOUTH);
   titlepanel.setBackground(Color.yellow);
   weekpanel.setBackground(Color.green);
   titlepanel.add(titlabel);
  
   panel2.setBackground(Color.green);
   panel3.setBackground(Color.green);
   panel1.add(titlepanel);
   panel1.add(weekpanel);
   panel4.add(initbutton);
   panel4.add(okbut);
   panel4.add(alterbut);
   panel4.add(restbut);
  
   initbutton.addActionListener(this);
   alterbut.addActionListener(this);
   okbut.addActionListener(this);
   restbut.addActionListener(this);
   inititem.addActionListener(this);
   displayitem.addActionListener(this);
   alteritem.addActionListener(this);
   resetitem.addActionListener(this);

   int i,j;
   for(i=0;i<8;i++)
    label1[i]=new JLabel("");
   label1[0]=new JLabel("节次");
   label1[1]=new JLabel("    星期一");
   label1[2]=new JLabel("    星期二");
   label1[3]=new JLabel("    星期三");
   label1[4]=new JLabel("    星期四");
   label1[5]=new JLabel("    星期五");
   label1[6]=new JLabel("    星期六");
   label1[7]=new JLabel("    星期日");
   label1[0].setBackground(Color.blue);
   for(i=0;i<8;i++)
    weekpanel.add(label1[i]);
   for(i=1;i<12;i++)
    for(j=1;j<8;j++)
    {
     text1[i][j]=new JTextArea(20,2);
     panel2.add(text1[i][j]);
    }
   
   label2[0]=new JLabel(1+"(8:00-8:45)");
   label2[1]=new JLabel(2+"(8:55-9:40)");
   label2[2]=new JLabel(3+"(10:00-10:45)");
   label2[3]=new JLabel(4+"(10:55-11:40)");
   label2[4]=new JLabel(5+"(14:30-15:15)");
   label2[5]=new JLabel(6+"(15:25-16:10)");
   label2[6]=new JLabel(7+"(16:30-17:15)");
   label2[7]=new JLabel(8+"(17:25-18:10)");
   label2[8]=new JLabel(9+"(19:00-19:45)");
   label2[9]=new JLabel(10+"(19:55-20:40)");
   label2[10]=new JLabel(11+"(20:50-21:35)");
   for(i=0;i<11;i++)
    panel3.add(label2[i]);
   now=new Date();
   d=DateFormat.getDateTimeInstance();
   time=d.format(now);
   timelabel=new JLabel(time);
   titlepanel.add(timelabel);
   this.setVisible(true);
   this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   //this.setResizable(false);
   this.addWindowListener(new winad());
    while(true)
    {
     now=new Date();
     d=DateFormat.getDateTimeInstance();
     time=d.format(now);
     //System.out.println(now);
     timelabel.setText(time);  
     try 
     {
      Thread.sleep(1000);
     }
     catch (InterruptedException e) 
     {
      e.printStackTrace();
     }
    }  
}

public void actionPerformed(ActionEvent e)
{

    int i,j,k;
    for(i=0;i<12;i++)
     for(j=0;j<8;j++)
      course[i][j]=new String("");

    try
    {
    	Class.forName("com.mysql.jdbc.Driver");
       String url="jdbc:mysql://192.168.1.118/week";
       String username="root";
       String password="root";       
       Connection conn= DriverManager.getConnection(url,username,password);
       
       Statement stmt=conn.createStatement();
       String sql=new String();
       ResultSet rs;
       ResultSetMetaData m;
       i=j=0;
       if(e.getSource()==initbutton||e.getSource()==inititem)
       {
        System.out.println("New ~~~~~");
        try{
        sql="create table 'coursetable'" +
           " ( 'week' int(11)," +
           "    'sequence' int(11)," +
           "    'course' varchar(30));";
        stmt.executeQuery(sql);
        for(i=1;i<12;i++)
      for(j=1;j<8;j++)
      { 
         sql="insert into(coursetable)values(j,i,'"+text1[i][j].getText()+"')";
         stmt.executeUpdate(sql);
      }
        
        }
        catch(java.sql.SQLException e1)
        {
         e1.printStackTrace();
         e.setSource(okbut);
         JOptionPane.showMessageDialog(null,"课程表数据库已存在，无需新建！","发生错误！",0);
         //System.out.println("java.sql.SQLException happened!");
        }
        initbutton.setVisible(false);
        inititem.setVisible(false);
       }
       if(e.getSource()==okbut||e.getSource()==displayitem)
       {
        sql="select * from coursetable order by sequence asc,week asc"; 
        rs=stmt.executeQuery(sql);
        m=rs.getMetaData();
        int columns=m.getColumnCount();
        String cours;
        rs.next();
        for(i=1;i<12;i++)
         for(j=1;j<8;j++)
         {
          cours=rs.getString(j);
          text1[i][j].setText(cours);
          rs.next();
         }          
           //System.out.println("显示");
       }
       if(e.getSource()==restbut||e.getSource()==resetitem)
       {
           k=JOptionPane.showConfirmDialog(null,"确定要重置吗?","重置",0 );
           if(k==0)
           {
            //System.out.println("重置");
            sql="update coursetable set course=null";
            stmt.executeUpdate(sql);
          for(i=1;i<12;i++)
           for(j=1;j<8;j++)
             text1[i][j].setText("");
           }
         
       }
       if(e.getSource()==alterbut||e.getSource()==alteritem)
       {
        k=JOptionPane.showConfirmDialog(null,"确定修改吗？","修改",0);
        if(k==0)
        {
          for(i=1;i<12;i++)
           for(j=1;j<8;j++)
           { 
              sql="update coursetable set course='"+text1[i][j].getText()+"'where week='"+j+"' and sequence='"+i+"'";
              stmt.executeUpdate(sql);
           }
         // System.out.println("修改");
        }

       }
    }
       catch (ClassNotFoundException e1) 
    {
        e1.printStackTrace();
    } 
    catch (SQLException e2) 
    {
        e2.printStackTrace();
    }
}
class winad extends WindowAdapter
{
   public void windowClosing(ActionEvent e)
   {
    System.exit(0);
   }
}

public static void main(String args[]) 
{
   try   
   {   
             //UIManager.setLookAndFeel(new   com.sun.java.swing.plaf.motif.MotifLookAndFeel());   
             //UIManager.setLookAndFeel(new   com.sun.java.swing.plaf.metal.MetalLookAndFeel());   
             UIManager.setLookAndFeel(new   com.sun.java.swing.plaf.windows.WindowsLookAndFeel());   
         }   
         catch   (Exception   e)   {e.printStackTrace   ();}  

   CourseTable ct=new CourseTable();
  
}
}